package com.meilingcloud.biobank.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.HashUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.biobank.domain.*;
import com.meilingcloud.biobank.domain.dto.SampleCreateSaveDto;
import com.meilingcloud.biobank.domain.dto.SampleImportDto;
import com.meilingcloud.biobank.domain.dto.SampleQueryDto;
import com.meilingcloud.biobank.domain.dto.StatisticDto;
import com.meilingcloud.biobank.domain.vo.*;
import com.meilingcloud.biobank.mapper.SampleBackupMapper;
import com.meilingcloud.biobank.service.*;
import com.meilingcloud.common.constant.Constants;
import com.meilingcloud.common.core.redis.RedisCache;
import com.meilingcloud.common.enums.*;
import com.meilingcloud.common.exception.CustomException;
import com.meilingcloud.common.utils.DateUtils;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.common.utils.SequenceUtils;
import com.meilingcloud.common.utils.uuid.UUID;
import com.meilingcloud.system.domain.SysAttach;
import com.meilingcloud.system.domain.SysSeq;
import com.meilingcloud.system.domain.vo.SeqDetailVo;
import com.meilingcloud.system.service.ISysAttachService;
import com.meilingcloud.system.service.ISysSeqService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 样本复份 Service业务层处理
 *
 * @author chenl
 * @date 2021-03-08
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class SampleBackupServiceImpl extends ServiceImpl<SampleBackupMapper, SampleBackup> implements ISampleBackupService {

    /**
     * 是否为当前用户操作的样本(出入库等操作时筛选)
     */
    public static final String SAMPLE_UPDATE_BY_ME = "0";
    private final ISourceService sourceService;
    private final ISampleGroupService sampleGroupService;
    private final ISampleTypeService sampleTypeService;
    private final ISourceTypeService sourceTypeService;
    private final ISampleService sampleService;
    private final RedisCache redisCache;
    private final ISysAttachService sysAttachService;
    private final ISysSeqService sysSeqService;

    @Override
    public List<SampleBackup> queryList(SampleQueryDto sampleQueryDto) {
        if (StrUtil.isNotBlank(sampleQueryDto.getOwn()) && SAMPLE_UPDATE_BY_ME.equals(sampleQueryDto.getOwn())) {
            sampleQueryDto.setUpdateBy(SecurityUtils.getUsername());
        }
        return baseMapper.queryList(sampleQueryDto);
    }

    /**
     * 查询样本复份列表,包含已删除
     *
     * @param sampleQueryDto sampleQueryDto 查询条件
     * @return result
     */
    @Override
    public List<SampleBackup> queryListIncludeDeleted(SampleQueryDto sampleQueryDto) {
        if (StrUtil.isNotBlank(sampleQueryDto.getOwn()) && SAMPLE_UPDATE_BY_ME.equals(sampleQueryDto.getOwn())) {
            sampleQueryDto.setUpdateBy(SecurityUtils.getUsername());
        }
        return baseMapper.queryListIncludeDeleted(sampleQueryDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveSample(SampleCreateSaveDto sampleCreateSaveDto) {
        Source source = new Source();
        BeanUtils.copyProperties(sampleCreateSaveDto, source);
        if (ObjectUtil.isNotEmpty(sampleCreateSaveDto.getFormDataSaveDto())) {
            source.setFormData(JSON.toJSONString(sampleCreateSaveDto.getFormDataSaveDto().getData()));
            source.setFormDesc(JSON.toJSONString(sampleCreateSaveDto.getFormDataSaveDto().getDesc()));
        }
        if (ObjectUtil.isNotEmpty(source.getSourceId())) {
            source.setUpdateBy(SecurityUtils.getUsername());
        } else {
            source.setCreateBy(SecurityUtils.getUsername());
        }
        sourceService.saveOrUpdate(source);
        // 写入附件表
        List<SysAttach> attaches = source.getAttaches();
        if (CollectionUtil.isNotEmpty(attaches)) {
            List<SysAttach> attachList = attaches.stream().map(item -> {
                item.setRelateId(String.valueOf(source.getSourceId()));
                item.setModule(AttachModule.SAMPLE_SOURCE.getCode());
                item.setCreateBy(SecurityUtils.getUsername());
                return item;
            }).collect(Collectors.toList());
            sysAttachService.saveOrUpdateBatch(attachList);
        }

//        // 根据编码规则生成样本编号
//        String currentDate = DateUtils.getCurDate();
//        SampleType sampleType = sampleTypeService.getById(sampleCreateSaveDto.getSampTypeId());
//        Long num = redisCache.getIncrementNum(Constants.INCREASE_SERIAL_NUM_KEY + currentDate);
//        StringBuffer sampleCode = new StringBuffer();
//        SysSeq sysSeq = sysSeqService.getActive();
//        if (ObjectUtil.isNotNull(sysSeq)) {
//            List<SeqDetailVo> seqDetailVos = JSON.parseArray(sysSeq.getSeqDetail(), SeqDetailVo.class);
//            if (CollectionUtil.isNotEmpty(seqDetailVos)) {
//                for (int i = 0; i < seqDetailVos.size(); i++) {
//                    SeqDetailVo vo = seqDetailVos.get(i);
//                    if (vo.getCode().equals(SeqRuleType.SAMPLE_CODE.getCode()) && ObjectUtil.isNotEmpty(sampleType)) {
//                        sampleCode.append(sampleType.getSampTypeCode());
//                    }else if (vo.getCode().equals(SeqRuleType.SOURCE_TYPE_CODE.getCode())) {
//                        SourceType sourceType = sourceTypeService.getById(sampleCreateSaveDto.getSourceTypeId());
//                        sampleCode.append(sourceType.getSourceTypeCode());
//                    }else if (vo.getCode().equals(SeqRuleType.SOURCE_CODE.getCode())) {
//                        sampleCode.append(sampleCreateSaveDto.getSourceCode());
//                    }else if (vo.getCode().equals(SeqRuleType.TIMESTAMP.getCode())) {
//                        sampleCode.append(currentDate);
//                    }else if (vo.getCode().equals(SeqRuleType.SERIAL_NUM.getCode())) {
//                        sampleCode.append(SequenceUtils.getSequence(num));
//                    }else if (vo.getCode().equals(SeqRuleType.FIXED_FIELD.getCode())) {
//                        sampleCode.append(vo.getValue());
//                    }else if (vo.getCode().equals(SeqRuleType.RANDOM_NUM.getCode())) {
//                        sampleCode.append(HashUtil.rsHash(currentDate+ UUID.fastUUID()));
//                    }
//                    if (i < seqDetailVos.size() - 1) {
//                        sampleCode.append(sysSeq.getDelimiter());
//                    }
//                }
//            }else {
//                if (ObjectUtil.isNotEmpty(sampleType)) {
//                    sampleCode.append(sampleType.getSampTypeCode());
//                    sampleCode.append(sysSeq.getDelimiter());
//                }
//                sampleCode.append(currentDate + SequenceUtils.getSequence(num));
//            }
//        }else {
//            if (ObjectUtil.isNotEmpty(sampleType)) {
//                sampleCode.append(sampleType.getSampTypeCode());
//                sampleCode.append("-");
//            }
//            sampleCode.append(currentDate + SequenceUtils.getSequence(num));
//        }

        Sample sample = new Sample();
        BeanUtils.copyProperties(sampleCreateSaveDto, sample);
        sample.setSampSourceId(source.getSourceId());
//        sample.setSampCode(sampleCode.toString());
        sample.setSampCode(sampleCreateSaveDto.getSampCode());
        sample.setCreateBy(SecurityUtils.getUsername());
        sampleService.save(sample);

        List<SampleBackup> sampleBackups = new ArrayList<>();
        for (int i = 0; i < sampleCreateSaveDto.getSampBackupCount(); i++) {
            SampleBackup sampleBackup = new SampleBackup();
            BeanUtils.copyProperties(sampleCreateSaveDto, sampleBackup);
            sampleBackup.setSampSourceId(source.getSourceId());
            sampleBackup.setSampBackupCode(sample.getSampCode() + "-" + (i + 1));
            sampleBackup.setSampBackupSeq(i + 1);
            sampleBackup.setSampId(sample.getSampId());
            sampleBackup.setSampSourceTypeId(sampleCreateSaveDto.getSourceTypeId());
            sampleBackup.setCreateBy(SecurityUtils.getUsername());
//            if (sampleCreateSaveDto.getTubeType().equals(TubeType.PASTE_CODE.getCode())) {
//                 贴码管 条码/二维码 = (时间戳+UUID).hashCode()
//                sampleBackup.setQrCode(HashUtil.rsHash(currentDate+ UUID.fastUUID()) + "");
            sampleBackup.setQrCode(sampleBackup.getSampBackupCode());
//            }

            sampleBackups.add(sampleBackup);
        }
        boolean result = this.saveBatch(sampleBackups);

        return result;
    }

    @Override
    public List<SampleBackup> handleResult(List<SampleBackup> sampleBackups) {
        List<Long> sourceIds = sampleBackups.stream().map(SampleBackup::getSampSourceId).collect(Collectors.toList());
        List<Source> sources = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(sourceIds)) {
            sources = sourceService.listByIds(sourceIds);
        }
        List<Source> finalSources = sources;
        boolean isNotEmpty = CollectionUtil.isNotEmpty(finalSources);
        List<SampleBackup> collect = sampleBackups.stream().map(item -> {
            if (isNotEmpty) {
                finalSources.stream().filter(i -> ObjectUtil.equal(i.getSourceId(), item.getSampSourceId()))
                        .findFirst()
                        .map(i -> {
                            item.setSampSourceName(i.getSourceName());
                            item.setSampSourceCode(i.getSourceCode());
                            return item;
                        }).orElse(null);
            }
            SampleGroup sampleGroup = sampleGroupService.queryById(item.getSampGroupId());
            if (ObjectUtil.isNotEmpty(sampleGroup)) {
                item.setSampGroupName(sampleGroup.getSampGroupName());
            }
            SampleType sampleType = sampleTypeService.queryById(item.getSampTypeId());
            if (ObjectUtil.isNotEmpty(sampleType)) {
                item.setSampTypeName(sampleType.getSampTypeName());
                item.setSampTypeIcon(sampleType.getSampTypeImgUrl());
            }
            SourceType sourceType = sourceTypeService.queryById(item.getSampSourceTypeId());
            if (ObjectUtil.isNotEmpty(sourceType)) {
                item.setSampSourceTypeName(sourceType.getSourceTypeName());
            }
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public SampleBackup queryByQrCode(SampleBackup sampleBackup) {
        if (StringUtils.isBlank(sampleBackup.getQrCode())) {
            throw new CustomException("条码/二维码不能为空");
        }
        LambdaQueryWrapper<SampleBackup> lqw = Wrappers.lambdaQuery();
        lqw.eq(SampleBackup::getQrCode, sampleBackup.getQrCode());
        if (StringUtils.isNotBlank(sampleBackup.getSampState())) {
            lqw.eq(SampleBackup::getSampState, sampleBackup.getSampState());
        }
        lqw.last("limit 1");
        SampleBackup sample = this.getOne(lqw);
        if (ObjectUtil.isEmpty(sample)) {
            return sample;
        }
        Source source = sourceService.getById(sample.getSampSourceId());
        if (ObjectUtil.isNotEmpty(source)) {
            sample.setSampSourceName(source.getSourceName());
        }
        SourceType sourceType = sourceTypeService.getById(sample.getSampSourceTypeId());
        if (ObjectUtil.isNotEmpty(sourceType)) {
            sample.setSampSourceTypeName(sourceType.getSourceTypeName());
        }
        SampleGroup sampleGroup = sampleGroupService.getById(sample.getSampGroupId());
        if (ObjectUtil.isNotEmpty(sampleGroup)) {
            sample.setSampGroupName(sampleGroup.getSampGroupName());
        }
        SampleType sampleType = sampleTypeService.getById(sample.getSampTypeId());
        if (ObjectUtil.isNotEmpty(sampleType)) {
            sample.setSampTypeName(sampleType.getSampTypeName());
        }
        return sample;
    }

    @Override
    public List<SampleBackup> querySameSamples(SampleBackup sampleBackup) {
        Long sampId = sampleBackup.getSampId();
        List<SampleBackup> sampleBackups = this.list(new LambdaQueryWrapper<SampleBackup>().eq(SampleBackup::getSampId, sampId)
                .eq(SampleBackup::getSampState, SampStatue.NORMAL.getCode()));
        List<SampleBackup> collect = sampleBackups.stream()
                .filter(item -> ObjectUtil.notEqual(item.getSampBackupId(), sampleBackup.getSampBackupId()))
                .collect(Collectors.toList());
        return collect;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String importSample(List<SampleImportDto> sampleImportDtos, Boolean isUpdateSupport) {
        if (CollectionUtil.isEmpty(sampleImportDtos)) {
            throw new CustomException("导入样本数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SampleImportDto dto : sampleImportDtos) {
            try {
                SourceType sourceType = sourceTypeService.queryByCode(dto.getSampSourceTypeCode());
                if (ObjectUtil.isEmpty(sourceType)) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、样本源类型编码 " + dto.getSampSourceTypeCode() + " 不存在");
                    continue;
                }

                Source source = sourceService.queryByCode(dto.getSampSourceCode());
                if (ObjectUtil.isEmpty(source)) {
                    source = new Source();
                    source.setSourceCode(dto.getSampSourceCode());
                    source.setSourceName(dto.getSourceName());
                    source.setSourceDesc(dto.getSourceDesc());
                    source.setSourceTypeId(sourceType.getSourceTypeId());
                    source.setFollowState(dto.getFollowState());
                    source.setCreateBy(SecurityUtils.getUsername());
                    sourceService.save(source);
                } else if (isUpdateSupport) {
                    source.setSourceName(dto.getSourceName());
                    source.setSourceDesc(dto.getSourceDesc());
                    source.setFollowState(dto.getFollowState());
                    source.setUpdateBy(SecurityUtils.getUsername());
                    sourceService.saveOrUpdate(source);
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、样本源编号 " + dto.getSampTypeCode() + " 已存在");
                    continue;
                }

                SampleType sampleType = sampleTypeService.queryByCode(dto.getSampTypeCode());
                if (ObjectUtil.isEmpty(sampleType)) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、样本类型编码 " + dto.getSampTypeCode() + " 不存在");
                    continue;
                }
                SampleGroup sampleGroup = sampleGroupService.queryByCode(dto.getSampGroupCode());
                if (ObjectUtil.isEmpty(sampleGroup)) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、样本组编码 " + dto.getSampGroupCode() + " 不存在");
                    continue;
                }

                String currentDate = DateUtils.getCurDate();
                Sample sample = new Sample();
                sample.setSampSourceId(source.getSourceId());
                sample.setSampTypeId(sampleType.getSampTypeId());
                if (StringUtils.isNotBlank(dto.getSampBackupCode())) {
                    // 导入信息存在样本编号
                    sample.setSampCode(dto.getSampBackupCode());
                } else {
                    // 导入信息中不存在样本编号 根据编码规则生成样本编号
                    Long num = redisCache.getIncrementNum(Constants.INCREASE_SERIAL_NUM_KEY + currentDate);
                    StringBuffer sampleCode = new StringBuffer();
                    SysSeq sysSeq = sysSeqService.getActive();
                    if (ObjectUtil.isNotNull(sysSeq)) {
                        List<SeqDetailVo> seqDetailVos = JSON.parseArray(sysSeq.getSeqDetail(), SeqDetailVo.class);
                        if (CollectionUtil.isNotEmpty(seqDetailVos)) {
                            for (int i = 0; i < seqDetailVos.size(); i++) {
                                SeqDetailVo vo = seqDetailVos.get(i);
                                if (vo.getCode().equals(SeqRuleType.SAMPLE_CODE.getCode()) && ObjectUtil.isNotEmpty(sampleType)) {
                                    sampleCode.append(sampleType.getSampTypeCode());
                                } else if (vo.getCode().equals(SeqRuleType.SOURCE_TYPE_CODE.getCode())) {
                                    sampleCode.append(sourceType.getSourceTypeCode());
                                } else if (vo.getCode().equals(SeqRuleType.SOURCE_CODE.getCode())) {
                                    sampleCode.append(dto.getSampSourceCode());
                                } else if (vo.getCode().equals(SeqRuleType.TIMESTAMP.getCode())) {
                                    sampleCode.append(currentDate);
                                } else if (vo.getCode().equals(SeqRuleType.SERIAL_NUM.getCode())) {
                                    sampleCode.append(SequenceUtils.getSequence(num));
                                } else if (vo.getCode().equals(SeqRuleType.FIXED_FIELD.getCode())) {
                                    sampleCode.append(vo.getValue());
                                } else if (vo.getCode().equals(SeqRuleType.RANDOM_NUM.getCode())) {
                                    sampleCode.append(HashUtil.rsHash(currentDate + UUID.fastUUID()));
                                }
                                if (i < seqDetailVos.size() - 1) {
                                    sampleCode.append(sysSeq.getDelimiter());
                                }
                            }
                        } else {
                            if (ObjectUtil.isNotEmpty(sampleType)) {
                                sampleCode.append(sampleType.getSampTypeCode());
                                sampleCode.append(sysSeq.getDelimiter());
                            }
                            sampleCode.append(currentDate + SequenceUtils.getSequence(num));
                        }
                    } else {
                        if (ObjectUtil.isNotEmpty(sampleType)) {
                            sampleCode.append(sampleType.getSampTypeCode());
                            sampleCode.append("-");
                        }
                        sampleCode.append(currentDate + SequenceUtils.getSequence(num));
                    }
                    sample.setSampCode(sampleCode.toString());
                }
                sample.setSampQuantity(dto.getSampQuantity());
                sample.setSampUnit(dto.getSampUnit());
                sample.setSampBackupCount(dto.getSampBackupCount());
                sample.setCreateBy(SecurityUtils.getUsername());
                boolean r = sampleService.save(sample);
                if (!r) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、样本源编码 " + dto.getSampSourceCode()
                            + "、样本类型编码 " + dto.getSampTypeCode()
                            + "、样本源类型编码 " + dto.getSampSourceTypeCode()
                            + "、样本组编码 " + dto.getSampGroupCode()
                            + " 导入失败");
                    continue;
                }

                List<SampleBackup> sampleBackups = new ArrayList<>();
                for (int i = 0; i < dto.getSampBackupCount(); i++) {
                    SampleBackup sampleBackup = new SampleBackup();
                    sampleBackup.setSampId(sample.getSampId());
                    sampleBackup.setSampSourceId(sample.getSampSourceId());
                    sampleBackup.setSampSourceTypeId(sourceType.getSourceTypeId());
                    sampleBackup.setSampTypeId(sample.getSampTypeId());
                    sampleBackup.setSampGroupId(sampleGroup.getSampGroupId());
                    if (StringUtils.isNotBlank(dto.getSampBackupCode())) {
                        if (dto.getSampBackupCount() < 2) {
                            sampleBackup.setSampBackupCode(dto.getSampBackupCode());
                        } else {
                            sampleBackup.setSampBackupCode(dto.getSampBackupCode() + (i + 1));
                        }
                    } else {
                        sampleBackup.setSampBackupCode(sample.getSampCode() + "-" + (i + 1));
                    }
                    sampleBackup.setSampBackupSeq(i + 1);
                    sampleBackup.setSamplingTime(dto.getSamplingTime());
                    sampleBackup.setTubeType(dto.getTubeType());
                    sampleBackup.setSampQuantity(dto.getSampQuantity());
                    sampleBackup.setSampUnit(dto.getSampUnit());
                    sampleBackup.setCreateBy(SecurityUtils.getUsername());
                    if (StringUtils.isNotBlank(dto.getQrCode())) {
                        sampleBackup.setQrCode(dto.getQrCode());
                    } else {
                        if (StringUtils.equals(dto.getTubeType(), TubeType.PASTE_CODE.getCode())) {
                            // 贴码管 条码/二维码 = (时间戳+UUID).hashCode()
                            sampleBackup.setQrCode(HashUtil.rsHash(currentDate + UUID.fastUUID()) + "");
                        }
                    }

                    sampleBackups.add(sampleBackup);
                }
                boolean result = false;
                if (CollectionUtil.isNotEmpty(sampleBackups)) {
                    result = this.saveBatch(sampleBackups);
                }
                if (result) {
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、样本源编码 " + dto.getSampSourceCode()
                            + "、样本类型编码 " + dto.getSampTypeCode()
                            + "、样本源类型编码 " + dto.getSampSourceTypeCode()
                            + "、样本组编码 " + dto.getSampGroupCode()
                            + " 导入成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、样本源编码 " + dto.getSampSourceCode()
                            + "、样本类型编码 " + dto.getSampTypeCode()
                            + "、样本源类型编码 " + dto.getSampSourceTypeCode()
                            + "、样本组编码 " + dto.getSampGroupCode()
                            + " 导入失败");
                }
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、样本源编码 " + dto.getSampSourceCode()
                        + "、样本类型编码 " + dto.getSampTypeCode()
                        + "、样本源类型编码 " + dto.getSampSourceTypeCode()
                        + "、样本组编码 " + dto.getSampGroupCode()
                        + " 导入失败: " + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<SampleStatisticVo> statisticByType() {
//        List<SampleType> sampleTypes = sampleTypeService.list();
//        if (CollectionUtil.isEmpty(sampleTypes)) {
//            return new ArrayList<>();
//        }
//        List<SampleStatisticVo> collect = sampleTypes.stream().map(item -> {
//            SampleStatisticVo statisticVo = new SampleStatisticVo();
//            statisticVo.setSampTypeName(item.getSampTypeName());
//            int total = this.count(new LambdaQueryWrapper<SampleBackup>().eq(SampleBackup::getSampTypeId, item.getSampTypeId()));
//            int usage = this.count(new LambdaQueryWrapper<SampleBackup>().eq(SampleBackup::getSampTypeId, item.getSampTypeId())
//                    .gt(SampleBackup::getSampFreezeThawCount, 0));
//            statisticVo.setTotal(total);
//            statisticVo.setUsage(usage);
//            return statisticVo;
//        }).collect(Collectors.toList());
//        return collect;
        return this.baseMapper.statisticByType(SampStatue.NORMAL.getCode());
    }

    @Override
    public List<SampleLifeCycleVo> getSampleLifeCycle(Long sampBackupId) {
        List<SampleLifeCycleVo> lifeCycle = this.baseMapper.getSampleLifeCycle(sampBackupId);
        List<SampleLifeCycleVo> collect = lifeCycle.stream().map(l -> {
            if (l.getType().equals(StockType.IN.getCode())) {
                l.setTypeName(StockType.IN.getInfo());
            } else if (l.getType().equals(StockType.OUT.getCode())) {
                l.setTypeName(StockType.OUT.getInfo());
            } else if (l.getType().equals(StockType.CLAIM.getCode())) {
                l.setTypeName(StockType.CLAIM.getInfo());
            } else if (l.getType().equals(StockType.RETURN.getCode())) {
                l.setTypeName(StockType.RETURN.getInfo());
            } else if (l.getType().equals(StockType.DESTROY.getCode())) {
                l.setTypeName(StockType.DESTROY.getInfo());
            } else if (l.getType().equals(ShiftType.SAMPLE.getCode())) {
                l.setTypeName(ShiftType.SAMPLE.getInfo());
            }
            return l;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<StatisticVo> chartByType(String sampState) {
        return this.baseMapper.chartByType(sampState);
    }

    @Override
    public List<StatisticVo> chartBySourceType(String sampState) {
        return this.baseMapper.chartBySourceType(sampState);
    }

    @Override
    public List<StatisticVo> chartByGroup(String sampState) {
        return this.baseMapper.chartByGroup(sampState);
    }

    @Override
    public List<StatisticVo> chartByCreator(String sampState) {
        return this.baseMapper.chartByCreator(sampState);
    }

    @Override
    public List<StatisticVo> chartBySamplingTime(StatisticDto dto) {
        return this.baseMapper.chartBySamplingTime(dto);
    }

    @Override
    public List<StatisticVo> chartByStockTime(StatisticDto dto) {
        return this.baseMapper.chartByStockTime(dto);
    }

    @Override
    public List<StatisticVo> chartByExpired() {
        return this.baseMapper.chartByExpired();
    }

    @Override
    public List<SampTypeReportVo> reportBySampType(StatisticDto dto) {
        return this.baseMapper.reportBySampType(dto);
    }

    @Override
    public List<SourceTypeReportVo> reportBySourceType(StatisticDto dto) {
        return this.baseMapper.reportBySourceType(dto);
    }

    @Override
    public List<SampGroupReportVo> reportBySampGroup(StatisticDto dto) {
        return this.baseMapper.reportBySampGroup(dto);
    }

    @Override
    public Integer statisticSampleCount() {
        return this.count(new LambdaQueryWrapper<SampleBackup>().eq(SampleBackup::getSampState, SampStatue.NORMAL.getCode()));
    }

    @Override
    public SampleBackup getInfoById(Long sampleBackupId) {
        return this.baseMapper.getInfoById(sampleBackupId);
    }

    public boolean updateSampleBackup(SampleBackup sampleBackup) {
        return this.baseMapper.updateSampleBackup(sampleBackup);
    }

}
